package com.apex.springcloud.controller;

import com.apex.springcloud.pojo.Dept;
import com.apex.springcloud.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Administrator
 * @version 1.0
 * @program: springcloud-parant
 * @Classname DeptController
 * @description TODO
 * @date 2021/7/14 22:52
 */

@RestController
@RequestMapping("/dept")
public class DeptController {

    @Autowired
    private DeptService service;

    @Autowired
    private DiscoveryClient client;

    @GetMapping("/discovery")
    public Object discovery(){
        //获得微服务列表清单
        List<String> list = client.getServices();
        System.out.println("client.getServices()==>"+list);

        //得到一个具体的微服务
        List<ServiceInstance> serviceInstancesList = client.getInstances("springcloud-provider-dept");
        for(ServiceInstance serviceInstance : serviceInstancesList){
            System.out.println(
                    serviceInstance.getServiceId()+"\t"+
                    serviceInstance.getHost()+"\t"+
                    serviceInstance.getPort()+"\t"+
                    serviceInstance.getUri()
            );
        }
        return this.client;
    }


    @PostMapping("/add")
    public boolean addDept(@RequestBody Dept dept){
        return service.addDept(dept);
    }

    @GetMapping("/get/{id}")
    public Dept get(@PathVariable("id") Long id){
        return service.queryById(id);
    }

    @GetMapping("/list")
    public List<Dept> queryAll(){
        return service.queryAll();
    }


}
